/**
 * @class NG.controllers.InfoView
 * @extends Ext.Controller
 */
NG.controllers.InfoView = Ext.extend(Ext.Controller, {
	
	/**
	 * <b>options</b> param consists of<br>
	 * <li>command: NG.command.PutInfoEx
	 * <li>callback: Object. {successFn: Function, failureFn: Function}. Optional.
	 * 
	 * @private
	 * @params {Object} options
	 */
	doAction: function (options) {
		var command = options.command;
		var workspace = command.getWorkspace();
		
		function process(sessionID){
    		NGServiceRequester.executeCommand(command, sessionID, {
    	    	requestFinished: function (response) {
    	    		var putInfoExResponse = command.getResponse(response);
    	    		if (putInfoExResponse.isSuccessful()) {
    	    			alert("SUCCESS --> " + putInfoExResponse.getAffectedRecordCount());
    	    			Ext.dispatch({
    	    				controller: app.controllers.searchView,
    	    				action: 'refreshViewsAfterPutInfoEx',
    	    				command: command
    	    			});
    	    		} else {
    	    			alert(putInfoExResponse.getMessage());
    	    		}
    	    		if (options.callback && Ext.isFunction(options.callback.successFn)) {
    	    			options.callback.successFn.call();
    	    		}    	    		
    	    	},
    	    	requestFail: function (response){
    	    		if (options.callback && Ext.isFunction(options.callback.failureFn)) {
    	    			options.callback.failureFn.call();
    	    		}
    	    	}
    	    });    					
		}
		
		var session = NGServiceRequester.isAlreadyLoggedIn(workspace.getUsername(), workspace.getLoginUrl());
		if (session) {
			process(session.sessionID);
		} else {
			NGServiceRequester.login(ws.getUsername(), workspace.getPassword(), workspace.getLoginUrl(), {
				loginSuccess: function(sessionID) {
					process(sessionID);
				},
				loginFail: function(response) {
					alert("Login Failed");
				}
			});
		}
	},
	
	/**
	 * It is used for Update and delete putinfoex commands
	 * @private
	 * @param {Object} options
	 */
	createPutInfoExCommand: function (options){
		var infoDetail = options.infoDetail;
		var typeName = infoDetail.getParent().getFeatureName();
    	var getinfoex = app.views.searchView.getGetInfoExCommand();
    	var workspace = getinfoex.getWorkspace();
		var putInfoEx = new NG.command.PutInfoEx({
			workspace: workspace,
			workspaceName: workspace.getName(),
			runXmlUrl: workspace.getRunXmlUrl(),
			typeName: typeName,
			infoDetail: infoDetail
		});
		return putInfoEx;
	},
	
	/**
	 * <b>options</b> param consists of<br>
	 * <li>infoDetail: NG.models.InfoDetail
	 * 
	 * @param {Object} options
	 */
	deleteFAD: function (options) {
		console.log('deleteFAD');
		var putInfoExDelete = this.createPutInfoExCommand(options);
		putInfoExDelete.dmlType = 'delete';
		this.doAction({
			command: putInfoExDelete
		});
		/*
		alert(putInfoExDelete.validate()?"true":putInfoExDelete.getErrorStr());
		alert(putInfoExDelete.getDataXML());
		*/
		
	},
	
	viewFAD: function (options) {
		
	},
	
	/**
	 * Updates FAD according to information view on NG.info form panel.
	 * <b>options</b> params consists of<br>
	 * <li>modifiedFields: Array
	 * <li>coordinates: String
	 * <li>formPanel: Ext.Panel
	 * <li>callback: Object. {successFn: Function, failureFn: Function}. Optional.
	 * 
	 * @param {Object} options
	 */
	updateFAD: function (options) {
		var putInfoExUpdate = this.createPutInfoExCommand(options);
		putInfoExUpdate.dmlType = 'update';
		putInfoExUpdate.modifiedFields = options.modifiedFields;
		
		this.doAction({
			command: putInfoExUpdate,
			callback: options.callback
		});
		console.log("updateFAD --> " + putInfoExUpdate.getDataXML());
		
	},
	
	/**
	 * Inserts FAD according to information view on NG.info form panel.
	 * <b>options</b> params consists of<br>
	 * <li>fad: NG.models.Table FAD which matchs by selected category
	 * <li>modifiedFields: Array
	 * <li>coordinates: String
	 * <li>formPanel: Ext.Panel
	 * <li>callback: Object. {successFn: Function, failureFn: Function}. Optional.
	 * 
	 * @param {Object} options
	 */
	insertFAD: function (options) {
		console.log('insertFAD');

		var category = app.views.mapView.getSelectedCategory();
		var fad = options.fad;
		if (!category) {
			alert(MSG_SELECT_CATEGORY);
			return;
		}
		if (!fad) {
			alert("FAD tanımsız.");
			return; 
		}
		alert(options.coordinates);
		var typeName = fad.getTableName();
		var workspace = app.views.mapView.getWorkspace();
		var putInfoExInsert = new NG.command.PutInfoEx({
			modifiedFields: options.modifiedFields,
			workspace: workspace,
			workspaceName: workspace.getName(),
			runXmlUrl: workspace.getRunXmlUrl(),
			typeName: typeName,
			dmlType: 'insert',
			geometry: new NG.command.GEOMETRY({
				coordinates: options.coordinates,
				category: category
			})
		});		
		
		this.doAction({
			command: putInfoExInsert,
			callback: options.callback			
		});
		console.log("insertFAD --> " + putInfoExInsert.getDataXML());
	},
	
	/**
	 * View detail is the readonly mode of updateFAD. Same form is displayed except that only update button is invisible and 
	 * all fields are readonly.<br>
	 * 
	 * <b>options</b> param consists of<br>
	 * <li>infoDetail: NG.models.InfoDetail.
	 * <li>editable: Boolean. For displaying this property must be false, for updating this property must be true.
	 * 
	 * @param {Object} options
	 */
	viewDetailOfFAD: function (options) {
		console.log('viewDetailOfFAD');
		var form = new NG.info();
		form.initForm({
			infoDetail: options.infoDetail,
			editable: options.editable
		}).show('pop');
	},
	
	prepareComboListItems: function(options) {
		var workspace =  app.views.mapView.workspace;
		var component = options.component;
		
		var lookupReq = new NG.command.GetLookupXml({
			workspace: workspace,
			runXmlUrl: workspace.getRunXmlUrl(),
			workspaceName: workspace.getName(),
			keyField: options.field.getComboProperties().keyFieldName,
			valueField: options.field.getComboProperties().displayFieldName,
			tableName: options.field.getComboProperties().fadName
		});
		
		var session = NGServiceRequester.isAlreadyLoggedIn(workspace.getUsername(), workspace.getLoginUrl());
		if (session) {

    		NGServiceRequester.executeCommand(lookupReq, session.getSessionID(), {
            	requestFinished: function (response) {
            		
            		var lookupResponse = new NG.response.GetLookupXml({
            			xmlData: response
            		});
            		
            		component.setData(lookupResponse.getLookupTable().getRecords());

            	},
            	requestException: function (exception) {
            		component.setData([{text: "Servis Hatası", value: 0}]);
            		component.setDisabled(true);
            	},
            	requestFail: function (response) {
            		alert("lookup failed!");
            	}
    		});			
			
		}
	}
});

/**
 * @singleton app.controllers.infoView
 */
app.controllers.infoView = new NG.controllers.InfoView();